package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UacStaff;
import com.csun.cmny.provider.model.domain.UacStaffStatus;
import com.csun.cmny.provider.model.dto.resident.ResidentDto;
import com.csun.cmny.provider.model.dto.resident.ResidentQueryDto;
import com.csun.cmny.provider.model.dto.staff.*;
import com.csun.cmny.provider.model.exceptions.UacBizException;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.support.collections.RedisSet;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/resident", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacResidentController extends BaseController {
    @Resource
    private UacResidentService uacResidentService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @PostMapping(value = "/add")
    public Wrapper addResident(@RequestBody ResidentDto residentDto, BindingResult br) {

        Integer result = uacResidentService.addResident(residentDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    @PostMapping(value = "/edit")
    public Wrapper editResident(@RequestBody ResidentDto residentDto,
                                 BindingResult br) {
        Integer result = uacResidentService.updateResident(residentDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    @PostMapping(value = "/showById/{residentId}")
    public Wrapper showResident(@PathVariable Long residentId) {
        Preconditions.checkArgument(residentId != null, cmnyMessageSource
                .getMessage("resident.id.null", null));
        ResidentDto residentDto = uacResidentService.showResident(residentId);
        if(residentDto == null){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), residentDto);
    }

    @PostMapping(value = "/deleteById/{residentId}")
    public Wrapper deleteResident(@PathVariable Long residentId) {
        // 校验Id是否为空
        Preconditions.checkArgument(residentId != null, cmnyMessageSource
                .getMessage("resident.id.null", null));
        Integer result = uacResidentService.deleteResidentById(residentId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

    @PostMapping(value = "/listPage")
    public Wrapper listPage(@RequestBody ResidentQueryDto residentQueryDto) {
        residentQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = uacResidentService.listPage(residentQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), pageInfo);
    }


}
